﻿using Abp.Application.Services.Dto;
using Abp.Authorization;
using Microsoft.AspNetCore.Mvc;
using shenghua.Authorization;
using shenghua.Basics.CommodityGuarantees;
using shenghua.Basics.CommodityGuaranteeSetting.Dto;
using shenghua.Common.Dto;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace shenghua.Basics.CommodityGuaranteeSetting
{
    [AbpAuthorize(PermissionNames.CommodityGuaranteeManagement)]
    public class CommodityGuaranteeSettingAppService: shenghuaAppServiceBase, ICommodityGuaranteeSettingAppService
    {
        private readonly CommodityGuaranteeManager _commodityGuaranteeManager;

        public CommodityGuaranteeSettingAppService(
            CommodityGuaranteeManager commodityGuaranteeManager
            )
        {
            _commodityGuaranteeManager = commodityGuaranteeManager;
        }

        public async Task<CommodityGuaranteeDto> Create(CreateCommodityGuaranteeDto input)
        {
            CommodityGuarantee item = new CommodityGuarantee
            {
                Name = input.Name,
                DisplayName = input.DisplayName,
                Description = input.Description,
                Enabled = input.Enabled
            };
            await _commodityGuaranteeManager.Add(item);
            return ObjectMapper.Map<CommodityGuaranteeDto>(item);
        }

        public async Task<CommodityGuaranteeDto> Update(UpdateCommodityGuaranteeDto input)
        {
            var item = await _commodityGuaranteeManager.Get(input.Id);
            item.DisplayName = input.DisplayName;
            item.Description = input.Description;
            await _commodityGuaranteeManager.Update(item);
            return ObjectMapper.Map<CommodityGuaranteeDto>(item);
        }

        public async Task SortUp(EntityDto input)
        {
            await _commodityGuaranteeManager.SortUp(input.Id);
        }

        public async Task SortDown(EntityDto input)
        {
            await _commodityGuaranteeManager.SortDown(input.Id);
        }

        public async Task SetEnabled(EntityDto input)
        {
            await _commodityGuaranteeManager.Enabled(input.Id, true);
        }

        public async Task SetDisabled(EntityDto input)
        {
            await _commodityGuaranteeManager.Enabled(input.Id, false);
        }

        [HttpPost]
        public async Task Delete(DeleteDto input)
        {
            foreach(var id in input.Ids)
            {
                await _commodityGuaranteeManager.Delete(id);
            }
        }

        public async Task<List<CommodityGuaranteeDto>> GetAll()
        {
            var items = await _commodityGuaranteeManager.GetAll();
            return ObjectMapper.Map<List<CommodityGuaranteeDto>>(items);
        }
    }
}
